package com.medical.admin.hospital.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.medical.admin.hospital.common.dto.HospitalDto;
import com.medical.admin.hospital.common.dto.NavigateInterior;
import com.medical.admin.hospital.common.vo.HospitalVo;
import com.medical.admin.hospital.common.vo.PageVo;
import com.medical.admin.hospital.entity.Hospital;
import com.medical.admin.hospital.mapper.HospitalMapper;
import com.medical.admin.hospital.service.HospitalService;
import com.qf.medical.ex.BaseException;
import com.qf.medical.ex.ServiceException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service
public class HospitalServerImpl implements HospitalService {

    @Resource
    private HospitalMapper hospitalMapper;

    @Resource
    private ObjectMapper objectMapper;

    /**
     * 动态分页查询医院
     * @param hospitalName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageVo<HospitalVo> list(String hospitalName,int pageNum,int pageSize) {
        Page<Hospital> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Hospital> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(hospitalName!=null,Hospital::getHospitalName,hospitalName);
        Page<Hospital> hospitalPage = hospitalMapper.selectPage(page, queryWrapper);
        //判断数据是否为空
        if (ObjectUtil.isEmpty(hospitalPage.getRecords())){
            throw new BaseException(ResponseCode.HOSPITAL_NOT_EXIST_ERROR);
        }
        PageVo<HospitalVo> hospitals = new PageVo<>();
        hospitals.setRows(BeanConvertUtils.convertListTo(hospitalPage.getRecords(),HospitalVo::new));
        hospitals.setTotal(hospitalPage.getRecords().size());
        return hospitals;
    }

    /**
     * 新增医院信息
     * @param hospitalDto
     * @return
     */
    @Override
    public Integer insert(HospitalDto hospitalDto) {
        //通过医院姓名判断医院是否存在
        List<Hospital> hos = findHospital(hospitalDto);
        if (ObjectUtil.isNotEmpty(hos)){
            throw  new BaseException(ResponseCode.HOSPITAL_EXIST_ERROR);
        }
        Hospital hospital = BeanUtil.copyProperties(hospitalDto, Hospital.class);
        try {
            hospital.setNavigateInterior(objectMapper.writeValueAsString(hospitalDto.getNavigateInterior()));
        } catch (JsonProcessingException e) {
            throw new BaseException(ResponseCode.SYS_ERROR);
        }
        return hospitalMapper.insert(hospital);
    }

    /**
     * 修改医院信息
     * @param hospitalDto
     * @return
     */
    @Override
    public Integer update(HospitalDto hospitalDto) {
        List<Hospital> hos = findHospital(hospitalDto);
        Optional.ofNullable(hos).orElseThrow(()->new BaseException(ResponseCode.HOSPITAL_NOT_EXIST_ERROR));
        Hospital hospital = BeanUtil.copyProperties(hospitalDto, Hospital.class);
        if (hospital.getState()!=1){
            throw new BaseException(ResponseCode.HOSPITAL_NOT_EXIST_ERROR);
        }
        try {
            hospital.setNavigateInterior(objectMapper.writeValueAsString(hospitalDto.getNavigateInterior()));
        } catch (JsonProcessingException e) {
            throw new BaseException(ResponseCode.SYS_ERROR);
        }
        return hospitalMapper.updateById(hospital);
    }

    /**
     *  删除医院信息
     * @param hospitalId
     * @return
     */
    @Override
    public int delete(Long hospitalId) {
        Hospital hospital = hospitalMapper.selectById(hospitalId);
        Optional.ofNullable(hospital).orElseThrow(()->new BaseException(ResponseCode.HOSPITAL_NOT_EXIST_ERROR));
        hospital.setState(0);
        return hospitalMapper.updateById(hospital);
    }

    /**
     * 查询医院信息详情
     * @param hospitalId
     * @return
     */
    @Override
    public HospitalVo select(Long hospitalId) {
        Hospital hospital = hospitalMapper.selectById(hospitalId);
        if(ObjectUtil.isEmpty(hospital)){
            throw new BaseException(ResponseCode.HOSPITAL_NOT_EXIST_ERROR);
        }
        if (hospital.getState()!=1){
            throw new BaseException(ResponseCode.HOSPITAL_NOT_EXIST_ERROR);
        }
        HospitalVo hospitalVo = BeanConvertUtils.convertTo(hospital,HospitalVo::new);
        try {
            hospitalVo.setNavigateInterior(objectMapper.readValue(hospital.getNavigateInterior(), NavigateInterior.class));
        } catch (JsonProcessingException e) {
            throw new BaseException(ResponseCode.SYS_ERROR);
        }
        return  hospitalVo;
    }

    /**
     * 查询医院信息
     * @param hospitalDto
     * @return
     */
    public List<Hospital> findHospital(HospitalDto hospitalDto){
        //通过医院姓名判断医院是否存在
        LambdaQueryWrapper<Hospital> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Hospital::getHospitalName,hospitalDto.getHospitalName());
        return hospitalMapper.selectList(queryWrapper);
    }
}
